using Net5Dll;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Net5From
{
    public partial class ChangeRETC : Form
    {
        Point StartPoint;
        Point StopPoint;
        Point FristPoint = new Point(0, 0);
        Task Draw;
        bool DrawState;
        bool MouseUpState = false;
        Bitmap bitmap;
        Button button;
        public ChangeRETC()
        {
            InitializeComponent();
            DrawState = true;
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
        }

        private void ChangeRETC_Load(object sender, EventArgs e)
        {
            bitmap = UserTool.GetWindow(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
            this.BackgroundImage = bitmap;
            this.Width = Screen.PrimaryScreen.Bounds.Width;
            this.Height = Screen.PrimaryScreen.Bounds.Height;
            this.Location = new Point(0, 0);
            button = new Button();
            button.Text = "确认";
            button.Click += Button_Click;
            Draw = Task.Factory.StartNew(DrawRETC);
        }

        private void Button_Click(object sender, EventArgs e)
        {
           
        }

        private void ChangeRETC_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                if (StartPoint != FristPoint && StopPoint != FristPoint)
                {
                    StartPoint = FristPoint;
                    StopPoint = FristPoint;
                    this.Refresh();
                    return;
                }
                if (Draw != null && Draw.Status == TaskStatus.Running)
                {
                    DrawState = false;
                    Draw.Wait();
                }
                this.Close();
            }
            else if (e.Button == MouseButtons.Left)
            {
                this.Controls.Clear();
                StartPoint = e.Location;
                MouseUpState = true;
            }
        }
        private void DrawRETC()
        {
            while (DrawState)
            {
                if (MouseUpState)
                {
                    StopPoint = Control.MousePosition;
                    this.Invoke(new Action(() =>
                    {
                        this.Refresh();
                    }));
                }

                Thread.Sleep(66);
            }
        }

        private void ChangeRETC_MouseUp(object sender, MouseEventArgs e)
        {
            MouseUpState = false;
            if (StartPoint== StopPoint)
            {
                return;
            }
            this.Controls.Add(button);
            button.Location = new Point(StopPoint.X - button.Width, StopPoint.Y + 2);
        }

        private void ChangeRETC_Paint(object sender, PaintEventArgs e)
        {
            var g = e.Graphics;
            g.DrawImage(bitmap, new PointF(0, 0));
            if (DrawState)
            {
                int x = StartPoint.X > StopPoint.X ? StopPoint.X : StartPoint.X;
                int y = StartPoint.Y > StopPoint.Y ? StopPoint.Y : StartPoint.Y;
                int width = Math.Abs(StartPoint.X - StopPoint.X);
                int height = Math.Abs(StartPoint.Y - StopPoint.Y);
                g.DrawRectangle(new Pen(Color.Red), new Rectangle(x, y, width, height));
            }
            g.DrawRectangle(new Pen(Color.Blue, 3), new Rectangle(0, 0, this.Width, this.Height));
        }
    }
}
